package io.github.talelin.sleeve.controller.v1;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.talelin.sleeve.dto.BannerDTO;
import io.github.talelin.sleeve.entity.Banner;
import io.github.talelin.sleeve.service.BannerService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.PositiveOrZero;
/**
 * @Description: 简单的增删改查
 */
@RestController
@RequestMapping("v1/banner")
@Validated
public class BannerController {


    @Resource
    private BannerService bannerService;

    @GetMapping("/page") //分页
    public Page getBanners(@RequestParam(name = "page",defaultValue = "0")
                           @Min(value = 0) Integer page,
                           @RequestParam(name ="count",defaultValue = "1")
                           @Min(value = 1) @Max(value = 30) Integer count ){

        Page<Banner> page1 = new Page(page,count);
        Page page2 =  bannerService.selectPage(page1);
        return page2;
    }
    @PostMapping("/update/{id}") //改
    public String updateBanner(@RequestBody BannerDTO bannerDTO,
                             @PathVariable(name = "id") @PositiveOrZero Long id){
        bannerService.updateById(id,bannerDTO);
        return "update";
    }
    @PostMapping("/insert") //增
    public String insertBanner(@RequestBody BannerDTO bannerDTO){
        bannerService.insertBanner(bannerDTO);
        return "添加成功";
    }
    @GetMapping("/delete/{id}") //删
    public String deleteBanner(@PathVariable(name = "id") Long id){
        bannerService.deleteById(id);
        return "删除成功";
    }
    @GetMapping("/redis")
    public String redisTest(){
        Long i = bannerService.getAllBanners();
        return "学生总数:"+i;
    }
}
